﻿using System.Collections.Generic;
using Web7.Extensions;
using System;

namespace Web7.Core.ViewResolvers
{
    /// <summary>
    /// 遗留视图解析器(处理老式浏览器的一些兼容性问题)
    /// </summary>
    public class LegacyViewResolver : IViewResolver
    {
        #region IViewResolver Members

        /// <summary>
        /// 视图解析
        /// </summary>
        public void Resolve(ViewResolverContext viewResolverContext, IList<string> viewRootPaths)
        {
            if (viewResolverContext == null)
                throw new ArgumentNullException("viewResolverContext");
            if (viewRootPaths == null)
                throw new ArgumentNullException("viewRootPaths");
            if (viewResolverContext.ViewRoot.IsNullOrWhiteSpace())
                return;

            if (viewResolverContext.RequestContext.HttpContext.Request.Browser.Browser.Contains("IE"))
            {
                if (viewResolverContext.RequestContext.HttpContext.Request.Browser.MajorVersion == 8)
                {
                    ResolveHacksFolder(viewRootPaths, "IE8");
                }
                if (viewResolverContext.RequestContext.HttpContext.Request.Browser.MajorVersion == 7)
                {
                    ResolveHacksFolder(viewRootPaths, "IE7");
                }
                else if (viewResolverContext.RequestContext.HttpContext.Request.Browser.MajorVersion == 6)
                {
                    ResolveHacksFolder(viewRootPaths, "IE6");
                }
            }
        }

        #endregion

        public static void ResolveHacksFolder(IList<string> viewRootPaths, string foldername)
        {
            List<string> newViewPaths = new List<string>(viewRootPaths.Count);

            foreach (string viewRootPath in viewRootPaths)
                newViewPaths.Add(string.Format("{0}{1}/{2}", viewRootPath, viewRootPath.EndsWith("/") ? "Hacks" : "/Hacks", foldername));

            foreach (string viewRootPath in newViewPaths)
                viewRootPaths.Add(viewRootPath);
        }
    }
}
